const koa = require("koa")
const bodyParser = require("koa-bodyparser");
const app = new koa()
const cors = require("koa2-cors");
const router = require("koa-router")()
router.post("/login",async ctx=>{
    var {username,pwd} = ctx.request.body
    if(username=='zheng'&& pwd=='123'){
        ctx.cookies.set('loginAuth',true,{
            httpOnly:false //服务器端设置cookie让客户端可读
        })
        ctx.body ={
            code:200,
            auth:true,
            msg:'登录成功'
        }
    }else{
        ctx.body={
            code:400,
            msg:'用户名或密码错误'
        }
    }
})
router.post('/logout',async ctx=>{
    ctx.cookies.set('loginAuth','')
    ctx.body={
        code:200,
        msg:'退出登录'
    }
})

app.use(cors({
    origin:ctx=>{
        return ctx.headers.origin
    },
    credentials:true   //可以跨域访问cookie
}))
app.use(bodyParser())
app.use(router.routes())
app.listen(8000)